define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/free_clean',
                    add_url: 'check/free_clean/add',
                    edit_url: 'check/free_clean/edit',
                    del_url: 'check/free_clean/del',
                    multi_url: 'check/free_clean/multi',
                    table: 'free_clean',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-editone").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                showSearch: false,
                search: false,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
//                                        if (row.check == 1) {
//                                            return { checked: true };
//                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'id', title: __('申请编号'), sortable: true},
                        {
                            field: 'status', title: __('审核状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "2": __('待审核'),
                                "3": __('已审核'),
                            },
                            formatter: function (value, row) {
                                if (row.audit_status == 1 && row.status == 3) {
                                    return '<span style="color:green">已审核</span>';
                                } else if (row.audit_status == 2 && row.status == 3) {
                                    return '<span style="color:red">已审核</span>';
                                } else {
                                    return '待审核';
                                }
                            }
                        },
                        {field: 'proprietor_name', title: '业主姓名', operate: 'like'},
                        {field: 'phone', title: '联系电话', operate: 'like'},
                        {field: 'address', title: '所在地区', operate: false},
                        {
                            field: 'address1',  title: '所在地区', searchList: function (column) {
                                return Template('address1', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'plot_name', title: '电梯地址', operate: 'like'},
                        {field: 'elevator_no', title: '电梯编号', operate: 'like'},
                        {field: 'type_text', title: '电梯类型', operate: false},
                        {
                            field: 'audit_status', title: __('审核结果'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('审核成功'),
                                "2": __('审核失败'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '审核成功';
                                        break;
                                    case 2:
                                        value = '审核失败';
                                        break;

                                }
                                return value;
                            },
                            visible: false
                        },
                        {
                            field: 'clean_area', title: __('清洁范围'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('轿厢内部四个面'),
                                "2": __('轿厢内部四个面+地板+厅门'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '轿厢内部四个面';
                                        break;
                                    case 2:
                                        value = '轿厢内部四个面+地板+厅门';
                                        break;

                                }
                                return value;
                            }
                        },


                        {field: 'longitude', title: '经度', visible: false, operate: false},
                        {field: 'latitude', title: '维度', visible: false, operate: false},
                        {
                            field: 'accuracy', title: __('定位准确度'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "1": __('准确'),
                                "0": __('不准确'),
                            },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '准确';
                                        break;
                                    case 0:
                                        value = '不准确';
                                        break;

                                }
                                return value;
                            }
                        },
                        {field: 'saleman.real_name', title: '业务员', operate:false},
                        {field: 'salesman_id', title: '业务员', addclass: 'selectpage',
                            extend: 'data-source="check/free_clean/getSaleMan" data-field="title"',visible: false},

                        {
                            field: 'submit_check_time',
                            title: __('提审时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },

                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'location',
                                    text: __('修改定位'),
                                    title: __('修改定位'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/free_clean/update_location?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/free_clean/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'audit',
                                    text: __('审核'),
                                    title: __('审核'),
                                    classname: 'btn btn-xs btn-warning btn-audit btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/free_clean/audit?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'again_audit',
                                    text: __('重新审核'),
                                    title: __('重新审核'),
                                    classname: 'btn btn-xs btn-warning btn-audit btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/free_clean/again_audit?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'assign',
                                    text: __('指派业务员'),
                                    title: __('指派业务员'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/free_clean/do_assign?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 3) {
                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-again_audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                    $(table).data("operate-again_audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                if (row.lon) {
                                    $(table).data("operate-location", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-location", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/free_clean/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            // 选中多个
            $(document).on("click", ".go", function () {
                var free_clean_id = $('#free_clean_id').val();
                Fast.api.open("check/free_clean/audit?id=" + free_clean_id, '审核',{
                    area: ['100%', '100%'],
                    callback: function (data) {
                        console.log(data.id)
                        console.log(data.name)
                        $("#school_class_id").val(data.id)
                        $("#school_class").val(data.name)
                    }
                });
            });
            $(document).on("click", ".again", function () {
                var free_clean_id = $('#free_clean_id').val();
                Fast.api.open("check/free_clean/again_audit?id=" + free_clean_id, '重新审核',{ area: ['100%', '100%'],

                    callback: function (data) {
                        console.log(data.id)
                        console.log(data.name)
                        $("#school_class_id").val(data.id)
                        $("#school_class").val(data.name)
                    }
                },true);
            });

        },
        do_most_assign: function () {
            Controller.api.bindevent();
        },
        update_location: function () {
            Controller.api.bindevent();
        },
        //审核
        audit: function () {
            Controller.api.bindevent();
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-2')
            //监听是开票的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //审核
        again_audit: function () {
            Controller.api.bindevent();
            var row = Config.row;
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-2')
            if(row.audit_status ==2){
                $('.bh').show();
            }else{
                $('.bh').hide();
            }
            //监听是驳回的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
